/*
 * Copyright (c) 2018. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
 * Morbi non lorem porttitor neque feugiat blandit. Ut vitae ipsum eget quam lacinia accumsan.
 * Etiam sed turpis ac ipsum condimentum fringilla. Maecenas magna.
 * Proin dapibus sapien vel ante. Aliquam erat volutpat. Pellentesque sagittis ligula eget metus.
 * Vestibulum commodo. Ut rhoncus gravida arcu.
 */

package com.aphaca.rs.web.api;

import com.aphaca.rs.web.domain.DemoRequest;
import com.aphaca.rs.web.domain.Response;
import com.aphaca.rs.web.domain.SumRequest;
import com.aphaca.rs.web.service.ResService;
import com.aphaca.rs.web.util.IoUtil;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.jboss.logging.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.io.FilenameFilter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@RestController
@RequestMapping("resource/")
public class ResController {

    @Autowired
    ResService resSer;

    Double total = 0.0;

    private Logger log = Logger.getLogger(this.getClass());

    @ApiOperation(value = "获取资源", notes = "获取", response = Response.class)
    @ApiResponses(value = { @ApiResponse(code = 400, message = "输入的参数无效"),
            @ApiResponse(code = 404, message = "找不到结果数据") })
    @RequestMapping(method = RequestMethod.POST,path = "base/")
    public String baseInfo(@RequestBody DemoRequest req) {
        //参数检查
        IoUtil.checkField(req);
        return IoUtil.out("0000","success",resSer.index());
    }

    @RequestMapping("/sso/")
    public Authentication getUser(Authentication authentication){
        log.info("resource:user ");
        log.info(authentication);
        log.info(authentication.getPrincipal().toString());
        return authentication;
    }

    @ApiOperation(value = "读取文件夹并统计金额", notes = "获取", response = Response.class)
    @ApiResponses(value = { @ApiResponse(code = 400, message = "输入的参数无效"),
            @ApiResponse(code = 404, message = "找不到结果数据") })
    @RequestMapping(method = RequestMethod.POST,path = "sum/")
    public String sum(@RequestBody SumRequest req) {
        //参数检查
        IoUtil.checkField(req);
        File dir = new File(req.getPath()); //被遍历的目录。
        total = 0.0;
        ArrayList li = new ArrayList();
        FilenameFilter list =new FilenameFilter() {
            @Override
            public boolean accept(File dir, String name) {
                if(name.endsWith(req.getSuffix())){
                    Pattern pattern = Pattern.compile("\\d+\\.\\d+");
                    Matcher matcher = pattern.matcher(name.replace(req.getSuffix(),""));
                    if(matcher.find()){
                        String tmp = matcher.group(0);
                        li.add(tmp);
                        total+= Double.valueOf(tmp);
                    }
                    return true;
                }
                return false;
            }
        };
        File[] files = dir.listFiles(list);
        HashMap out = new HashMap();
        out.put("total",total.intValue());
        out.put("details",li);
        return IoUtil.out("0000","success",out);
    }


}
